﻿using System.Net;
using Fvs_Service.Log;
using Fvs_Service.Net;

namespace Fvs_Service
{
    class Program
    {
        static void Main(string[] args)
        {
            LogPool.LogInformation(LogEvents.System,"version 1.0.3");
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            FvsMaid maid = new FvsMaid();
            maid.Start();
            while (true)
            {
                Thread.Sleep(1000);
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            LogPool.LogError(LogEvents.System,ex, $"Runtime terminating: {e.IsTerminating}");
        }

        static void Main1(string[] args)
        {

            //FvsMaid maid = new FvsMaid();
            LampMaid lampMaid = new LampMaid();
            SocketChannel lampChannel = lampMaid.AddLampConnectEndPoint(new IPEndPoint(IPAddress.Parse("101.200.81.217"), 60002), 100);
            //lampMaid.AddRelayConnectEndPoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60009));
            //SocketMaid clientMaid = new SocketMaid();
            //SocketChannel routerChannel = clientMaid.AddConnectEndPoint(new ProtocolHandler(), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7771));
            //SocketChannel noticeChannel= clientMaid.AddConnectEndPoint(new ProtocolHandler(), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7772));
            //maid.Start();
            lampMaid.Start();
            //clientMaid.Start();
            while (true)
            {
                Console.ReadLine();


                //collect
                //CollectStatus_Request request = new CollectStatus_Request();
                //var t = Protocol.Protocol.Request(request);
                //List<byte> buffer = new List<byte>();
                //SocketResult result = routerChannel.Send(null, t.Item1,
                //    r => r.ProtocolId == Convert.ToInt32(ProtocolId.CollectStatus + 1) && r.TimeStamp == t.Item2, null, buffer);

                //if (result == SocketResult.Success)
                //{
                //    CollectStatus_Response response = new CollectStatus_Response();
                //    ByteFormatter.Deserialize(response, buffer, ProtocolHead.HeadSize);

                //}
                //Console.WriteLine(result);


                //Shoot_Request request = new Shoot_Request
                //{
                //    Tag = "60009",
                //    Buffer = new List<byte>
                //    {
                //        0x55,
                //        0x01,
                //        0x10,
                //        0x00,
                //        0x00,
                //        0x00,
                //        0x00,
                //        0x66
                //    },
                //    ProtocolId = 0x10
                //};
                //Shoot_Request request = new Shoot_Request
                //{
                //    Tag = "100",
                //    Buffer = new List<byte>
                //    {
                //        0x81,
                //        0x7f,
                //        0x11,
                //        0xff
                //    },
                //    ProtocolId = 0x11
                //};
                //var t = Protocol.Request(request);
                //SocketResult result = channel.Send(null, t.Item1,
                //    r => r.ProtocolId == Convert.ToInt32(ProtocolId.Shoot + 1) && r.TimeStamp == t.Item2);
                //Console.WriteLine(result);

                int visibility = 1000;
                List<byte> buffer1 = new List<byte>
                {
                    0x90,
                    100,
                    0x06,
                    0x10,
                    0x00,
                    Convert.ToByte(visibility % 128),
                    Convert.ToByte(visibility / 128),
                    0x00,
                    0x00,
                    0x00,
                    0x00,
                    0x00,
                    0xff
                };
                //string content =
                //    "90 01 08 30 52 30 2C 44 6E 3D 30 30 30 44 2C 44 6D 3D 30 30 32 44 2C 44 78 3D 33 34 37 44 2C 53 6E 3D 30 30 30 2E 30 4D 2C 53 6D 3D 30 30 30 2E 33 4D 2C 53 78 3D 30 30 31 2E 31 4D 2C 54 61 3D 30 32 30 2E 38 43 2C 55 61 3D 30 35 39 2E 30 50 2C 50 61 3D 30 30 31 30 31 30 2E 39 48 2C";
                //List<byte> buffer1 = new List<byte> {0x90, 100, 0x08};
                //buffer1.AddRange(ByteConvert.ToBuffer(content));
                //buffer1.Add(0xff);
                lampChannel.Send(null, buffer1);
            }
        }
    }
}



